<?php

class Admin_FilamatriculaController extends App_Controller_Action
{
    private $_table     = 'FILA_MATRICULA';
    private $_pk        = 'FIL_ID';
    private $_order_col = 'FIL_ID';
    private $_order     = 'ASC';
    private $_sta_col   = '';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Filamatricula();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Filamatricula();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $where = $this->_pk . ' > 0';
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->group('CUR_ID')->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $this->form->getValues();
                    
                    if ($id == 0) $this->obj->insert($data);
                    else $this->obj->update($data, $this->_pk.' = ' . $id);
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
    
    public function novocursoAction(){
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_curso.js');
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $cur_id = $this->_getParam('id', 0);
        $obj = new Admin_Model_DbTable_Curso();
        $form = new Admin_Form_Curso();
        
        //$this->view->form = $form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = (int) $formData['CUR_ID'];//$this->form->getValue($this->_pk);
                    
                    $data = $formData;//$this->form->getValues();
                    //print_r($data);exit;
                    unset($data['FOR_ID']);
                    unset($data['submit']);
                    
                    if($data['CUR_TPOTMP'] == 1) $data['CUR_SEM'] = 0;
                    
                    $data['CUR_VLR'] = tools::money_to_float($data['CUR_VLR']); 
                    $data['CUR_DATPRO'] = Datetools::dma_amd($data['CUR_DATPRO']);
                    $data['CUR_PERDSC'] = tools::money_to_float($data['CUR_PERDSC']); 
                    $data['CUR_VLRPRO'] = tools::money_to_float($data['CUR_VLRPRO']); 
                    
                    $data['MTL_ID'] = implode(',', $data['MTL_ID']);
                        
                    $erro = 0;
                    if ($id == 0){
                        $data['CUR_VAGMAT'] = $data['CUR_NMRMAXALU']; // vagas disponíveis
                        $obj->insert($data);
                        $id =  $obj->getAdapter()->lastInsertId(); 
                        
                        // fila
                        $where = 'CUR_ID ='.$cur_id;
                        $fila = $this->obj->fetchAll($where);
                        foreach($fila as $fil){
                            $mat = new Admin_Model_DbTable_Matricula();
                            $datamat = array();
                            $datamat['MAT_STA'] = 1;
                            $datamat['CUR_ID']  = $id;
                            // atualiza matricula com novo curso e status
                            $mat->update($datamat, 'MAT_ID = '.$fil['MAT_ID']);                            
                            // deleta item da fila de matricula
                            $this->obj->delete('MAT_ID = '.$fil['MAT_ID']);
                        }
                        
                    }
                    else {
                        $obj->update($data, $this->_pk.' = ' . $id);
                    }
                    
                    $obj2 = new Admin_Model_DbTable_Cursoforma();
                    $obj2->delete('CUR_ID = '.(int)$id);
                    foreach($formData['FOR_ID'] as $for)
                    {   
                        $data = array( 'CUR_ID'=> $id, 'FOR_ID'=> $for);
                        $obj2->insert($data);
                    }
                    
                    echo '<script>alert("Novo curso criado com sucesso!");</script>';
                    $this->_helper->redirector('index');
                } else {
                    //$form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$form->populate($formData);
                $this->view->data = $formData;
            }
        } else{   
            if ($cur_id > 0) {
                $data = $obj->fetchRow('CUR_ID = ' . $cur_id)->toArray();
                $data['CUR_VLR'] = tools::float_to_money($data['CUR_VLR']); 
                $data['CUR_DATPRO'] = Datetools::amd_dma($data['CUR_DATPRO']);
                $data['CUR_VLRPRO'] = tools::float_to_money($data['CUR_VLRPRO']); 
                $data['CUR_PERDSC'] = tools::float_to_money($data['CUR_PERDSC']); 

                // zera dados
                $data['CUR_ID'] = 0;
                $data['CUR_TRM'] = 0;
                
                //$form->populate($data);
                $this->view->data = $data;
            }
        }
    }
    
    public function matriculasAction(){
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
       
        $cur = $this->_getParam('cur_id', 0);
        $where = ' CUR_ID = ' . $cur;
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function editmatriculaAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_matricula.js');
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $id = $this->_getParam('id', 0);
        $form = new Admin_Form_Matricula();
        $this->view->form = $form;
        
        if ($id > 0) {
            
            $obj = new Admin_Model_DbTable_Matricula();
            $data = $obj->fetchRow('MAT_ID = ' . $id)->toArray();
            
            $obj2 = new Admin_Model_DbTable_Aluno();
            $alu = $obj2->fetchRow('ALU_ID = ' . $data['ALU_ID'])->toArray();
            $data['ALU_NOM'] = $alu['ALU_NOM'];

            $obj2 = new Admin_Model_DbTable_Curso();
            $cur = $obj2->fetchRow('CUR_ID = ' . $data['CUR_ID'])->toArray();
            $data['CUR_VLR'] = tools::float_to_money($cur['CUR_VLR']);
            $form->populate($data);
        }
    }
    
    
}






